# -*- coding: utf-8 -*-
# @Time    : 2021/8/31 13:56
# @Author  : BrandonGT
# @FileName: test.py
# 1. 导入包和模块
# from PyQt5.QtWidgets import *
# from PyQt5.QtCore import *
# import sys
#
# class new(QWidget):
#     def __init__(self):
#         super().__init__()
#         self.setWindowTitle("新窗口")
#         self.resize(200, 200)
#
#     def setup_ui(self):
#         self.show()
#
# # 2. 创建一个应用程序对象
# app = QApplication(sys.argv)
#
# # 3. 创建控件
# window = QWidget()
#
# # 3.1 设置控件
# window.setWindowTitle("")
# window.resize(500, 500)
#
# btn = QPushButton(window)
# btn.resize(100, 50)
# btn.setText("创建新的窗口")
#
# w1 = new()
#
# btn.clicked.connect(w1.setup_ui)
# # 3.2 展示控件
# window.show()
#
# # 4. 应用程序执行，进入消息循环
# sys.exit(app.exec_())

# import sys
# from PyQt5 import QtGui
# from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QToolTip, QMessageBox, QLabel
#
# class Window2(QMainWindow):                           # <===
#     def __init__(self):
#         super().__init__()
#         self.setWindowTitle("Window22222")
#
# class Window(QMainWindow):
#     def __init__(self):
#         super().__init__()
#
#         self.title = "First Window"
#         self.top = 100
#         self.left = 100
#         self.width = 680
#         self.height = 500
#
#         self.pushButton = QPushButton("Start", self)
#         self.pushButton.move(275, 200)
#         self.pushButton.setToolTip("<h3>Start the Session</h3>")
#
#         self.pushButton.clicked.connect(self.window2)              # <===
#
#         self.main_window()
#
#     def main_window(self):
#         self.label = QLabel("Manager", self)
#         self.label.move(285, 175)
#         self.setWindowTitle(self.title)
#         self.setGeometry(self.top, self.left, self.width, self.height)
#         self.show()
#
#     def window2(self):                                             # <===
#         self.w = Window2()
#         self.w.show()
#         self.hide()
#
# if __name__ == "__main__":
#     app = QApplication(sys.argv)
#     window = Window()
#     sys.exit(app.exec())

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Created on 2019年12月11日
@author: Irony
@site: http://127.0.0.1 , https://github.com/892768447
@email: 892768447@qq.com
@file:
@description:
"""

from random import choice
import string

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QPainter, QFont, QIcon
from PyQt5.QtWidgets import QWidget, QMenu, QApplication


__Author__ = 'Irony'
__Copyright__ = 'Copyright (c) 2019'
__Version__ = 1.0

Style = '''
QMenu {
    /* 半透明效果 */
    /* background-color: rgba(255, 255, 255, 230); */
    background-color: white;
    border: none;
    border-radius: 4px;
}
#alphaMenu {
    background-color: rgba(255, 255, 255, 230);
}

QMenu::item {
    border-radius: 4px;
    /* 这个距离很麻烦需要根据菜单的长度和图标等因素微调 */
    padding: 8px 48px 8px 16px;
    background-color: transparent;
}

/* 鼠标悬停和按下效果 */
QMenu::item:selected {
    /* 半透明效果 */
    /* background-color: rgba(232, 232, 232, 232); */
    background-color: rgb(232, 232, 232);
}
#alphaMenu::item:selected {
    background-color: rgba(232, 232, 232, 100);
}

/* 禁用效果 */
QMenu::item:disabled {
    background-color: transparent;
}

/* 图标距离左侧距离 */
QMenu::icon {
    left: 15px;
}

/* 分割线效果 */
QMenu::separator {
    height: 1px;
    background-color: rgb(232,236,243);
}
'''


class Window(QWidget):

    def __init__(self, *args, **kwargs):
        super(Window, self).__init__(*args, **kwargs)
        self.resize(400, 300)
        self.initMenu()

    def contextMenuEvent(self, event):
        self._contextMenu.exec_(event.globalPos())

    def hello(self):
        QApplication.instance().aboutQt()

    def getIcon(self):
        # 测试模拟图标
        pixmap = QPixmap(16, 16)
        pixmap.fill(Qt.transparent)
        painter = QPainter()
        painter.begin(pixmap)
        painter.setFont(QFont('Webdings', 11))
        painter.drawText(0, 0, 16, 16, Qt.AlignCenter,
                         choice(string.ascii_letters))
        painter.end()
        return QIcon(pixmap)

    def initMenu(self):
        self._contextMenu = QMenu(self)
        # 背景透明
        self._contextMenu.setAttribute(Qt.WA_TranslucentBackground)
        # 无边框、去掉自带阴影
        self._contextMenu.setWindowFlags(
            self._contextMenu.windowFlags() | Qt.FramelessWindowHint | Qt.NoDropShadowWindowHint)

        self._contextMenu.addAction('菜单1', self.hello)
        self._contextMenu.addAction('菜单菜单2', self.hello).setEnabled(False)
        self._contextMenu.addAction(self.getIcon(), '菜单3', self.hello)
        self._contextMenu.addSeparator()

        # 二级菜单
        menu2 = QMenu('菜单菜单菜单4', self._contextMenu)  # 背景透明
        menu2.setObjectName('alphaMenu')  # 半透明演示
        menu2.setAttribute(Qt.WA_TranslucentBackground)
        # 无边框、去掉自带阴影
        menu2.setWindowFlags(menu2.windowFlags() |
                             Qt.FramelessWindowHint | Qt.NoDropShadowWindowHint)
        menu2.addAction(self.getIcon(), '子菜单1')
        menu2.addAction(self.getIcon(), '子菜单2')
        menu2.addAction(self.getIcon(), '子菜单3')
        self._contextMenu.addMenu(menu2)

        self._contextMenu.addAction(self.getIcon(), '菜单5', self.hello)
        self._contextMenu.addAction(self.getIcon(), '菜单6', self.hello)


if __name__ == '__main__':
    import sys
    import cgitb
    cgitb.enable(1, None, 5, '')
    app = QApplication(sys.argv)
    app.setStyleSheet(Style)
    w = Window()
    w.show()
    sys.exit(app.exec_())